package cn.sxt.yxl.users.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminDao;
import cn.sxt.yxl.users.dao.IARoleDao;
import cn.sxt.yxl.users.pojo.AAdmin;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUsersService;
@Service("userService")
public class UserServiceImpl implements IUsersService{

	@Resource
	private IAAdminDao adminDao;
	@Resource
	private IARoleDao roleDao;
	
	/*--------------------- 管理员模块开始---------------- */
	@Override
	public AAdmin findOneAdminService(Map<String, Object> condMap) {
		
		return adminDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneAdminService(AAdmin admin) {
		JSONObject resultJson = new JSONObject();
		int res = adminDao.saveOne(admin);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneAdminService(AAdmin admin) {
		JSONObject resultJson = new JSONObject();
		int res = adminDao.updateOne(admin);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneAdminService(AAdmin admin) {
		JSONObject resultJson = new JSONObject();
		int res = adminDao.deleteOne(admin);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admin.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<AAdmin> findCondAdminListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			/* 查询总记录数 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询数据 */
			List<AAdmin> adminList = adminDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminList;
		}
		return adminDao.findCondList(condMap);
	}
	/*--------------------- 管理员模块开始---------------- */
	
	/*--------------------- 角色模块开始---------------- */
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		
		return roleDao.findOne(condMap);
	}

	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.saveOne(role);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.updateOne(role);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.deleteOne(role);
		if(res>0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("code", "1");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "0");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<ARole> findCondRoleListService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			/* 查询总记录数 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询数据 */
			List<ARole> roleList = roleDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return roleDao.findCondList(condMap);
	}
	/*--------------------- 角色模块开始---------------- */

}
